home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / INPUTS.ZIP / TESTKEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  9.7 KB  |  283 lines

  1. /*///////////////////////////////////////////*/
  2. /*              T E S T K E Y . C            */
  3. /*  Compilation instructions:                */
  4. /*                                           */
  5. /*  MICROSOFT 5.0                            */
  6. /*     cl -c -ms testkey.c                   */
  7. /*     link testkey inputs.lib;              */
  8. /*///////////////////////////////////////////*/
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <dos.h>
  12. #include "input.h"
  13.  
  14. /*////////////////////////////////////////////////*/
  15. /* USER DEFINED EXCEPTION KEY HANDLER PROTOTYPES  */
  16. /*////////////////////////////////////////////////*/
  17. void StringHandler(char);
  18. void AlphaHandler(char);
  19. void LogicalHandler(char);
  20. void LongHandler(char);
  21. void DoubleHandler(char);
  22. void DateHandler(char);
  23. void nestedfunction(char);
  24. void StartHandler(char);
  25.  
  26. /*//////////////////////////////////////////*/
  27. /* FUNCTIONS THAT YOU CAN REPLACE FOR SPEED */
  28. /*//////////////////////////////////////////*/
  29. void   gettext(int startx, int endx,
  30.                int starty, int endy, unsigned char *buff);
  31. void   puttext(int startx, int endx,
  32.                int starty, int endy, unsigned char *buff);
  33. void   ClrLine(int);
  34.  
  35. /*///////////////////*/
  36. /* INPUT DATA FIELDS */
  37. /*///////////////////*/
  38. char string[21];
  39. char YesNo = YES;
  40. int width;
  41. long LongNum;
  42. double DoubleNum;
  43. int decimals;
  44. char datestring[] = "  /  /  ";  /* DATE STRING MUST BE IN THIS FORMAT */
  45. static char buffer[2000];        /* AN ARRAY TO HOLD VIDEO, SHOULD BE  */
  46. void main(void)                  /* 4000 IN SIZE FOR A COMPLETE SCREEN */
  47. {
  48.    clrscr();
  49.    printf("During the input of the data fields, press any of the function\n");
  50.    printf("keys to execute exception key handlers. The first function for\n");
  51.    printf("alphanumeric input has an example of nested input. Its handler\n");
  52.    printf("will call  the logical  input  function  which  in turn has an\n");
  53.    printf("exception handler of its own.  The program was  purposely kept\n");
  54.    printf("as simple  as possible  so you could  more easily  see how the\n");
  55.    printf("functions are used in the source code for this example program.\n");
  56.    printf("Structures  could  be used  for input  or  windows  popped  up,\n");
  57.    printf("but I'll leave  those function  to written by  you.  There are\n");
  58.    printf("plenty of  library routines that  do windows and colors in the\n");
  59.    printf("public  domain.  Those routines  combined with the routines in\n");
  60.    printf("the  INPUT.LIB file  will  greatly  enhance  your capabilities\n");
  61.    printf("in the programming arena.  While testing  the following  input\n");
  62.    printf("functions, at anytime press the function keys and the value of\n");
  63.    printf("the key will be displayed. This value can be used to determine\n");
  64.    printf("the action that your handler will take.\n\n");
  65.    printf("While answering the next prompt, press the function keys.\n\n");
  66.    printf("Continue with demo? [ ]\b\b");
  67.    YesNo = inputLogical(YesNo, StartHandler);
  68.    if (YesNo == NO) {
  69.       clrscr();
  70.       return;
  71.    } 
  72.  
  73.    clrscr();
  74.    gotoxy(2,0);
  75.    printf("Enter alphanumeric data: ");
  76.    inputString(string, sizeof string, StringHandler);
  77.    printf("\n\nString entered was: %s\n\n", string);
  78.    printf("Press any key to continue ...");
  79.    waitkey();
  80.  
  81.    clrscr();
  82.    gotoxy(2,0);
  83.    printf("Enter alpha input only: ");
  84.    inputAlpha(string, sizeof string, AlphaHandler);
  85.    printf("\n\nString entered was %s\n\n", string);
  86.    printf("Press any key to continue ...");
  87.    waitkey();
  88.  
  89.    clrscr();
  90.    gotoxy(2,0);
  91.    printf("Enter Y/N only: [ ]\b\b");
  92.    YesNo = inputLogical(YesNo, LogicalHandler);
  93.    printf("\n\nLogical input was: %c\n\n", YesNo);
  94.    printf("Press any key to continue ...");
  95.    waitkey();
  96.  
  97.    clrscr();
  98.    gotoxy(2,0);
  99.    printf("Enter a long integer: ");
  100.    LongNum = inputLong(11, LongHandler);
  101.    printf("\n\nNumber entered was: %ld\n\n", LongNum);
  102.    printf("Press any key to continue ...");
  103.    waitkey();
  104.  
  105.    width = 20;    /* THIS IS THE WIDTH OF THE DOUBLE NUMBER */
  106.    decimals = 4;  /* SET DECIMALS TO FOUR PLACES            */
  107.    clrscr();
  108.    gotoxy(2,0);
  109.    printf("Enter a double number: ");
  110.    DoubleNum = inputDouble(width, decimals, DoubleHandler);
  111.    printf("\n\nNumber entered was: %e\n\n", DoubleNum);
  112.    printf("Same double number is: %f\n\n", DoubleNum);
  113.    printf("Dollar amount is: $%.2f\n\n", DoubleNum);
  114.    printf("Press any key to continue ...");
  115.    waitkey();
  116.  
  117.    clrscr();
  118.    gotoxy(2,0);
  119.    printf("Enter a date MM/DD/YY: [  /  /  ]");
  120.    gotoxy(2,24); /* PUT CURSOR IN CORRECT PLACE ON SCREEN FOR INPUT */
  121.    inputDate(datestring, DateHandler);
  122.    printf("\n\nDate entered was: %s\n\n", datestring);
  123.  
  124.    printf("End of test key program ...\n\n");
  125.  
  126.    return;
  127. }
  128.  
  129. /*////////////////////////////////////////////////////////////////
  130. /* The following functions are stub functions.  These functions */
  131. /* could be written to save a portion  of the screen, display a */
  132. /* message, do a nested  input function, or whatever you please */
  133. /* based on the value of the exception key.  Exception keys are */
  134. /* defined as any key that is not normal input and is not a key */
  135. /* that  exits the  current field  being  input.  If  you  want */
  136. /* nothing  done,  simply  execute  a  return statement.  These */
  137. /* functions are  part of  the interface  to the input routines */
  138. /* and must exist.  See INPUT.DOC for more information.         */
  139. /*//////////////////////////////////////////////////////////////*/
  140.  
  141. void StartHandler(char excpt_key) {      /* This function demonstates   */
  142.    static char buffer[4000];             /* how your functions could    */
  143.    gettext(3,10,8,50, buffer);           /* save a portion of the       */
  144.    gotoxy(5,15);                         /* screen ...                  */
  145.    printf("Key value is %d", excpt_key);
  146.    gotoxy(6,15);
  147.    printf("Press any non function key ... ");
  148.    waitkey();
  149.  
  150.    puttext(3,10,8,50, buffer);            /* ... and then restore it    */
  151.    return;                                /* before your handler        */
  152. }                                         /* returns.                   */
  153.  
  154. void StringHandler(char excpt_key) {
  155.    char key;
  156.    key = YES;
  157.    gotoxy(0,0);
  158.    printf(
  159.   "Instring handler with key %d, F1-F10 for nested input. Exit? Y/N: [ ]\b\b",
  160.       excpt_key);
  161.    key = inputLogical(key, nestedfunction);
  162.    gotoxy(20,1);
  163.    if (key == YES)
  164.       printf("TRUE condition, press any key to continue ...", key);
  165.    else
  166.       printf("FALSE condition, press any key to continue ...", key);
  167.    waitkey();
  168.    ClrLine(20);
  169.    ClrLine(0);
  170.    return;
  171. }
  172.  
  173. void AlphaHandler(char excpt_key) {
  174.    gotoxy(0,0);
  175.    printf("Alpha handler executed with key %d, any key ...  ", excpt_key);
  176.    waitkey();
  177.    ClrLine(0);
  178.    return;
  179. }
  180.  
  181. void LogicalHandler(char excpt_key) {
  182.    gotoxy(0,0);
  183.    printf("Logical handler executed with key %d, any key ...  ", excpt_key);
  184.    waitkey();
  185.    ClrLine(0);
  186.    return;
  187. }
  188.  
  189. void LongHandler(char excpt_key) {
  190.    gotoxy(0,0);
  191.    printf("Integer handler executed with key %d, any key ...  ", excpt_key);
  192.    waitkey();
  193.    ClrLine(0);
  194.    return;
  195. }
  196.  
  197. void DoubleHandler(char excpt_key) {
  198.    gotoxy(0,0);
  199.    printf("Double handler executed with key %d, any key ...", excpt_key);
  200.    waitkey();
  201.    ClrLine(0);
  202.    return;
  203. }
  204.  
  205. void DateHandler(char excpt_key) {
  206.    gotoxy(0,0);
  207.    printf("Date handler executed with key %d, any key ...", excpt_key);
  208.    waitkey();
  209.    ClrLine(0);
  210.    return;
  211. }
  212.  
  213. void nestedfunction(char excpt_key) {
  214.    gotoxy(14,0);
  215.    printf("Nested function executed with key %d, any key ...  ", excpt_key);
  216.    waitkey();
  217.    ClrLine(14);
  218.    return;
  219. }
  220.  
  221. /* ////////////////////////////////////////////////////////////////*/
  222. /* The following functions can be rewritten or renamed to increase */
  223. /* the speed.  These were put here merely to show you how the      */
  224. /* keyboard handlers can call other screen handlers to save part   */
  225. /* of the screen and restore it or just erase one line.            */
  226. /* ////////////////////////////////////////////////////////////////*/
  227. void ClrLine(row) {
  228.    int i;
  229.    gotoxy(row, 0);
  230.    for (i = 0; i < 79; i++)
  231.       putch(' ');
  232.    return;
  233. }
  234.  
  235. /*//////////////////////////////////////////////////////////*/
  236. /* This is a very slow function that saves a portion of the */
  237. /* screen.  You can write  your own  preferably in assembly */
  238. /* language to speed this up.                               */
  239. /*//////////////////////////////////////////////////////////*/
  240. void gettext(int startx, int endx,
  241.              int starty, int endy, unsigned char *buff) {
  242.    union REGS r;
  243.    register int i, j;
  244.  
  245.    for (i = starty; i < endy; i++)
  246.       for (j = startx; j < endx; j++) {
  247.          gotoxy(j, i);
  248.          r.h.ah = 8;
  249.          r.h.bh = 0;
  250.          int86(0x10, &r, &r);
  251.          *buff++ = r.h.al;
  252.          *buff++ = r.h.ah;
  253.          putchar('█');
  254.       }
  255.  
  256.    for (i = starty+1; i < endy-1; i++)
  257.       for (j = startx+1; j < endx-1; j++) {
  258.          gotoxy(j, i);
  259.          putchar(' ');
  260.       }
  261. }
  262.  
  263. /*//////////////////////////////////////////////////////////////*/
  264. /* This is also a very slow routine to restore a portion of the */
  265. /* screen.  This is also a good candidate for assembly code.    */
  266. /*//////////////////////////////////////////////////////////////*/
  267. void puttext(int startx, int endx,
  268.         int starty, int endy, unsigned char *buff) {
  269.    union REGS r;
  270.    register int i, j;
  271.  
  272.    for (i = starty; i < endy; i++)
  273.       for (j = startx; j < endx; j++) {
  274.          gotoxy(j, i);
  275.          r.h.ah = 9;
  276.          r.h.bh = 0;
  277.          r.x.cx = 1;
  278.          r.h.al = *buff++;
  279.          r.h.bl = *buff++;
  280.          int86(0x10, &r, &r);
  281.       }
  282. }
  283.